// DlgCountDetail.cpp : implementation file
//

#include "stdafx.h"
#include "finance.h"
#include "financedef.h"
#include "CDataFile.h"
#include "DlgCountDetail.h"

#include "MainFrm.h"
#include "FinanceDoc.h"
#include "FinanceView.h"
#include "SearchView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgCountDetail dialog


CDlgCountDetail::CDlgCountDetail(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgCountDetail::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgCountDetail)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	m_pSearchView = NULL;
	m_pCmd = NULL;
	m_chSQLdate[0] = 0;
	m_chPurpose[0] = 0;
}


void CDlgCountDetail::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgCountDetail)
	DDX_Control(pDX, IDC_BTN_SAVESEARCH, m_btnSaveSearch);
	DDX_Control(pDX, IDOK, m_btnOk);
	DDX_Control(pDX, IDC_ED_DATERANGE, m_edDaterange);
	DDX_Control(pDX, IDC_ED_COUNTINFO, m_edCountinfo);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgCountDetail, CDialog)
	//{{AFX_MSG_MAP(CDlgCountDetail)
	ON_BN_CLICKED(IDC_BTN_SAVESEARCH, OnBtnSave2Search)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgCountDetail message handlers

// 
//--------------------------------------------------------------
BOOL CDlgCountDetail::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_btnSaveSearch.SetIcon(IDI_LEDON, IDI_LEDOFF);
	m_btnOk.SetIcon(IDI_EYEON, IDI_EYEOFF);
	
	// Subclassing
	m_chartDetail.SubclassDlgItem( IDC_CST_DETAILCHART, this );
	m_chartDetail.PrepareColors(CNSChartCtrl::SimpleColors);
	m_chartDetail.SetChartStyle( NSCS_BAR );
	m_chartDetail.EnableBarGridLines( TRUE );
	m_chartDetail.SetPieceValueFormat( NSCVAL_ABSVAL );


	//set back color and text color
	m_edDaterange.SetBackColor(RGB(255,255,255));
	m_edDaterange.SetTextColor(RGB(0,0,128));
	m_edCountinfo.SetBackColor(RGB(255,255,255));
	m_edCountinfo.SetTextColor(RGB(0,0,255));
	
	
	// Show detail info by purpose
	CountDetailofPurpose();

	// Show total and average
	CountOutline();

	UpdateData( FALSE );
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

// 
// public interface for Dlg invoke
//--------------------------------------------------------------
void CDlgCountDetail::SetSQLdaterange(LPTSTR pChSql)
{
	if( NULL != pChSql ) 
		strcpy( m_chSQLdate, pChSql );
}

//
// public interface for Dlg invoke
//--------------------------------------------------------------
void CDlgCountDetail::SetCOMPtr(_CommandPtr m_pComd)
{
	if( NULL != m_pComd )
		m_pCmd = m_pComd;
}

// 
// public interface for Dlg invoke
//--------------------------------------------------------------
void CDlgCountDetail::SetPurposeName(LPTSTR pChPurpose)
{
	if( NULL != pChPurpose )
		strcpy( m_chPurpose, pChPurpose );
}

// 
// 
//--------------------------------------------------------------
void CDlgCountDetail::CountDetailofPurpose()
{
	_RecordsetPtr	pRecSet;
	_variant_t		var;
	BOOL			bFlag = FALSE;
	DOUBLE			dbSum = 0;
	CHAR			chDate[50] = { 0 };
	CHAR			chDate1[50] = { 0 };
	CHAR			chSQL[512] = { 0 };
	CString			csTmp = "Detail of ";
	

	// <> reset
	m_chartDetail.Empty();

	// <> set title
	csTmp += m_chPurpose;
	m_chartDetail.SetTitle( csTmp );
	
	// <> Compose SQL 
	sprintf( chSQL, "SELECT Date,Amount FROM Account WHERE Purpose='%s' AND ", m_chPurpose );
	strcat( chSQL, m_chSQLdate );
	strcat( chSQL, " ORDER BY Date ASC" );
	try
	{
		m_pCmd->CommandText = chSQL;
		pRecSet = m_pCmd->Execute( NULL, NULL, adCmdText );
		
		while(!pRecSet->adoEOF)
		{
			var = pRecSet->GetCollect( (_variant_t)(long)0 );
			if(var.vt != VT_NULL)
				strcpy( chDate, (LPCSTR)_bstr_t(var) );
			else
				strcpy( chDate, "" );
			
			if( !bFlag ) 
				strcpy( chDate1, chDate );
			bFlag = TRUE;

			var = pRecSet->GetCollect( (_variant_t)(long)1 );
			switch( var.vt ) 
			{
			case VT_I4:	// long
				dbSum = var.lVal;
				break ;
			
			case VT_R4: // float
				dbSum = var.fltVal;
				break ;
			
			case VT_R8: // double
				dbSum = var.dblVal;
				break ;
				
			default :
				dbSum = 0;
				break ;
			}
			
			// into flow chart
			m_chartDetail.AddValue( dbSum, chDate );
			
			
			pRecSet->MoveNext();
		}

		// set date range info
		sprintf( chSQL, " %s   ------   %s ", chDate1, chDate );
		m_edDaterange.SetWindowText( chSQL );
		
		pRecSet->Close();
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
}

// 
// 
//--------------------------------------------------------------
void CDlgCountDetail::CountOutline()
{
	_RecordsetPtr	pRecSet;
	_variant_t		var;
	LONG			lCount = 0;
	DOUBLE			dbSum = 0;
	DOUBLE			dbAvg = 0;
	CHAR			chSQL[512] = { 0 };
	
	
	// <> Compose SQL 
	sprintf( chSQL, "SELECT SUM(Amount),AVG(Amount),COUNT(*) FROM Account WHERE Purpose='%s' AND ", m_chPurpose );
	strcat( chSQL, m_chSQLdate );
	try
	{
		m_pCmd->CommandText = chSQL;
		pRecSet = m_pCmd->Execute( NULL, NULL, adCmdText );
		
		if( !pRecSet->adoEOF )
		{
			var = pRecSet->GetCollect( (_variant_t)(long)0 );
			switch( var.vt ) 
			{
			case VT_I4:	// long
				dbSum = var.lVal;
				break ;
				
			case VT_R4: // float
				dbSum = var.fltVal;
				break ;
				
			case VT_R8: // double
				dbSum = var.dblVal;
				break ;
				
			default :
				dbSum = 0;
				break ;
			}
			
			var = pRecSet->GetCollect( (_variant_t)(long)1 );
			switch( var.vt ) 
			{
			case VT_I4:	// long
				dbAvg = var.lVal;
				break ;
				
			case VT_R4: // float
				dbAvg = var.fltVal;
				break ;
				
			case VT_R8: // double
				dbAvg = var.dblVal;
				break ;
				
			default :
				dbAvg = 0;
				break ;
			}

			var = pRecSet->GetCollect( (_variant_t)(long)2 );
			lCount = var.lVal;
			
			// set text
			sprintf( chSQL, " Total amount is %.2f , Total records is %ld , Average amount is %.2f ", dbSum, lCount, dbAvg );
			m_edCountinfo.SetWindowText( chSQL );
		}
		
		pRecSet->Close();
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
}

// 
// 
//--------------------------------------------------------------
void CDlgCountDetail::OnBtnSave2Search() 
{
	CDataFile		IniDF( MONEY_CFG );
	CHAR			chSQL[512] = { 0 };


	sprintf( chSQL, " Purpose='%s' AND ", m_chPurpose );
	strcat( chSQL, m_chSQLdate );
	
	IniDF.SetValue( "SQL", chSQL, "SQL segment for search", "SQL segment" );

	IniDF.Save();

	if( m_pSearchView )
		m_pSearchView->RefreshSearchView();
	
}

// 
// public interface for Dlg invoker
//--------------------------------------------------------------
void CDlgCountDetail::SetViewPtr(CSearchView *pView)
{
	if( pView != NULL ) 
		m_pSearchView = pView;
}
